﻿<sdk:Page x:Class="SampleBrowser.Views.SamplePageRotation" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
           xmlns:doc="http://schemas.firstfloorsoftware.com/documenttoolkit"
           xmlns:dte="http://schemas.firstfloorsoftware.com/documenttoolkit/extensions"
           xmlns:behaviors="clr-namespace:SampleBrowser.Behaviors"
           xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
           xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
           d:DesignWidth="640" d:DesignHeight="480"
           Title="Page Rotation">
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Grid.Resources>
            <ControlTemplate x:Key="RotatableDocumentViewerTemplate" TargetType="doc:DocumentViewer">
                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                    <ScrollViewer x:Name="ScrollViewer" Background="Transparent" BorderThickness="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                        <ItemsControl x:Name="Pages">
                            <i:Interaction.Behaviors>
                                <doc:PageVirtualizer />
                            </i:Interaction.Behaviors>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <!-- use a layout transformer to rotate pages. the transformer ensures the correct layout slot is used for rotated pages-->
                                    <toolkit:LayoutTransformer>
                                        <i:Interaction.Behaviors>
                                            <!-- behavior that controls the rotation -->
                                            <behaviors:LayoutRotateBehavior  />
                                        </i:Interaction.Behaviors>
                                        <Grid Width="{Binding DesiredPageContainerSize.Width}"
                                              Height="{Binding DesiredPageContainerSize.Height}">
                                            <Border Background="White" BorderBrush="{StaticResource SliderTrackBrush}" BorderThickness="1" Margin="4" HorizontalAlignment="Center" VerticalAlignment="Center" CacheMode="BitmapCache">
                                                <doc:FixedPageViewer FixedPage="{Binding FixedPage}" 
                                                                 FixedPageSize="{Binding FixedPageSize}"
                                                                 FixedPageError="{Binding FixedPageError}"
                                                                 Scale="{Binding DesiredPageScale}">
                                                    <i:Interaction.Behaviors>
                                                        <doc:NavigationBehavior TextContainer="{Binding TextContainer}" />
                                                    </i:Interaction.Behaviors>
                                                </doc:FixedPageViewer>
                                            </Border>

                                            <!-- the button that triggers page rotation, this button is managed by the LayoutRotateBehavior -->
                                            <Button x:Name="Rotate" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="32" Visibility="Collapsed">
                                                <Button.Content>
                                                    <Image Source="/SampleBrowser;component/Assets/Icons/Rotate.png" Width="16" Height="16" />
                                                </Button.Content>
                                            </Button>
                                            
                                        </Grid>
                                    </toolkit:LayoutTransformer>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Grid.Resources>

        <TextBlock x:Name="HeaderText" Style="{StaticResource HeaderTextStyle}" Text="Page rotation" />
        <TextBlock Grid.Row="1" Text="Hover with the mouse over a page and press the button to rotate individual pages" Margin="0,0,0,4" />
        <doc:DocumentDataSource x:Name="DataSource" LoadError="DataSource_LoadError" />

        <doc:DocumentViewer x:Name="Viewer"
                            Grid.Row="2"
                            Template="{StaticResource RotatableDocumentViewerTemplate}"
                            DocumentDataSource="{Binding ElementName=DataSource}"
                            ViewMode="{Binding SelectedViewMode, ElementName=ViewModePicker}">
        </doc:DocumentViewer>

        <StackPanel Grid.Row="3" Orientation="Horizontal" Margin="0,8,0,0" HorizontalAlignment="Center">
            <doc:HistoryNavigator History="{Binding History, ElementName=Viewer}" />
            <doc:PageNavigator x:Name="Navigator"
                               PageCount="{Binding PageCount, ElementName=Viewer}"
                               PageIndex="{Binding PageIndex, ElementName=Viewer, Mode=TwoWay}"
                               Margin="4,0,0,0" />
            <doc:ViewModePicker x:Name="ViewModePicker" Margin="4,0,0,0" />

            <Button Margin="4,0,0,0" Padding="7">
                <Button.Command>
                    <dte:OpenPdfOrXpsFromDiskCommand DocumentDataSource="{Binding ElementName=DataSource}" />
                </Button.Command>
                <Button.Content>
                    <Image Source="/SampleBrowser;component/Assets/Icons/open.png" />
                </Button.Content>
            </Button>

        </StackPanel>
    </Grid>
</sdk:Page>
